Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Migrations
Imports System.Linq

Namespace Migrations

    Friend NotInheritable Class Configuration 
        Inherits DbMigrationsConfiguration(Of PowerShellCronContext)

        Public Sub New()
            AutomaticMigrationsEnabled = True
            AutomaticMigrationDataLossAllowed = True
            ContextKey = "PowerShellCron.Service.PowerShellCronContext"
        End Sub

        Protected Overrides Sub Seed(context As PowerShellCronContext)
            '  This method will be called after migrating to the latest version.

            '  You can use the DbSet(Of T).AddOrUpdate() helper extension method 
            '  to avoid creating duplicate seed data. E.g.
            '
            '    context.People.AddOrUpdate(
            '       Function(c) c.FullName,
            '       New Customer() With {.FullName = "Andrew Peters"},
            '       New Customer() With {.FullName = "Brice Lambson"},
            '       New Customer() With {.FullName = "Rowan Miller"})

            'For Development -- remove when testing is complete
            Me.SeedEnums(context)
            Me.SeedDevData(context)
            
        End Sub

        Private Sub SeedDevData(context As PowerShellCronContext)

            'context.Jobs.AddOrUpdate(Function(x) x.Name, New Job With {.Name = "PSTest",
            '                               .Interval = 1,
            '                                       .IntervalType = IntervalTypes.Minute,
            '                                       .ForceDebug = True,
            '                                       .ForceVerbose = True,
            '                                       .Script = "Write-Host 'Starting PSTest..'; write-host 'Write-host'; Write-Verbose 'Write-Verbose'; Write-Warning 'write-warning'; write-debug 'write-debug'; Write-Error 'write-error'; Write-Output 'write-output'; [console]::writeline('console::writeline'); Write-Host 'PS Test ending..'"})

            'context.Jobs.AddOrUpdate(Function(x) x.Name, New Job With {.Name = "PSTest2",
            '                           .Interval = 1,
            '                           .IntervalType = IntervalTypes.Minute,
            '                           .ForceDebug = True,
            '                           .ForceVerbose = True,
            '                           .Script = "(1..12) | % {Write-Host ('Length: {0}' -f [math]::Pow(2,$_)); Write-Host (Get-RandomPassword -Length ([math]::Pow(2,$_)))}"})

            'context.Jobs.AddOrUpdate(Function(x) x.Name, New Job With {.Name = "PS Wait Test (10seconds)",
            '                           .Interval = 1,
            '                           .IntervalType = IntervalTypes.Minute,
            '                           .ForceDebug = True,
            '                           .ForceVerbose = True,
            '                           .Script = "Write-Host 'Beginning Wait Test...'; start-sleep -seconds 10; write-host 'Finished Wait Test!'"})

            'Dim sb As New Text.StringBuilder
            'sb.AppendLine("Param([string]$name, [int]$age)")
            'sb.AppendLine("Write-Host ('Hello World! says {0}.' -f $name)")
            'sb.AppendLine("Write-Host ('{0} is {1}' -f $name, $age)")
            'sb.AppendLine("Write-Host 'Finished!'")

            'context.Jobs.AddOrUpdate(Function(x) x.Name, New Job With {.Name = "PSParmTest",
            '                                           .Interval = 1,
            '                                           .IntervalType = IntervalTypes.Minute,
            '                                           .ForceDebug = True,
            '                                           .ForceVerbose = True,
            '                                           .Script = sb.ToString()})

            'context.Jobs.AddOrUpdate(Function(x) x.Name, New Job With {.Name = "ModuleTest",
            '                           .Interval = 1,
            '                           .IntervalType = IntervalTypes.Minute,
            '                           .ForceDebug = False,
            '                           .ForceVerbose = False,
            '                           .AllowMultiple = True,
            '                           .Script = "Import-Module \\sbcsystems.local\it\powershellmodules\pstools;Write-Host 'Getting Current Modules...'; Get-Module; Write-Host 'Getting All Modules...'; Get-Module -ListAvailable; $profile | select *; gci env:"})
        End Sub
        Private Sub SeedEnums(context As PowerShellCronContext)
            'context.Database.ExecuteSqlCommand("TRUNCATE TABLE dbo.EnumMapping")

            'Dim eList As New ArrayList
            'eList.Add(New With {.Table = "dbo.Jobs", .Column = "IntervalType", .Enum = GetType(PowerShellCron.Service.IntervalTypes)})
            'eList.Add(New With {.Table = "dbo.JobHistories", .Column = "JobHistoryType", .Enum = GetType(PowerShellCron.Service.JobHistoryTypes)})
            'eList.Add(New With {.Table = "dbo.Logs", .Column = "LogType", .Enum = GetType(PowerShellCron.Service.LogTypes)})
            'eList.Add(New With {.Table = "dbo.JobLogs", .Column = "JobLogType", .Enum = GetType(PowerShellCron.PSInterpeter.WriteDataSourceType)})

            'For Each el In eList
            '    For Each en As String In ([Enum].GetNames(el.Enum))
            '        Dim params(4) As Object
            '        params(0) = el.Table
            '        params(1) = el.Column
            '        params(2) = el.Enum.FullName
            '        params(3) = [Enum].Parse(el.Enum, en)
            '        params(4) = en
            '        context.Database.ExecuteSqlCommand("INSERT INTO dbo.EnumMapping (TableName, TableColumn, EnumType, EnumValue, EnumDescription) Values ({0},{1},{2},{3},{4})", params)
            '    Next
            'Next
        End Sub

    End Class

End Namespace
